package com.senven.api.utils;

import com.senven.api.pojo.Log;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

import static com.senven.api.utils.DateTimeConvert.dateToStr;

/**
 * @author LQP
 */
/**
 * 日志处理工具类
 */
public class LogUtil {
    public static Log getLog(String type, String operate, String result, HttpServletRequest request){
        Log log = new Log();
        Date date = new Date();
        String ip = IPUtil.getIpAddr(request);
        // 该属性在检查token的时候已经放入
        HttpSession session = request.getSession();
        String account = (String)session.getAttribute("account");
        // 若果登录失败则为null,设置为-1
        if(account == null){
            account = "非法用户";
        }
        log.setMessage(operate);
        log.setAccount(account);
        log.setCreateTime(date);
        log.setIp(ip);
        log.setLogname(type);
        log.setSucceed(result);
        return log;
    }
}
